//
//  AppointmentDetailController.m
//  HarmonizePub
//
//  Created by rzhy on 16/9/27.
//  Copyright © 2016年 Zy. All rights reserved.
//


/**
 * 你好！我是Zy，这个项目头号挖坑人.
 * 如果，你是按照流程来到这里的话.
 * 那么，恭喜你，你已经看完了这个功能.
 * 但是，如果你以为你已经看懂了的话.
 * 那么，你离换工作不远了...
 */


#import "AppointmentDetailController.h"
#import "Config.h"
#import "Patient.h"
#import "ScheduleDoctor.h"
#import "AppointmentRecordController.h"
#import "NoCardAppointmentRecordController.h"

@interface AppointmentDetailController ()
{
    Patient         *_patient;
    ScheduleDoctor  *_schedule;
}
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UILabel *depLabel;
@property (weak, nonatomic) IBOutlet UILabel *doctorLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *userLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UIButton *appointmentButton;
@property (weak, nonatomic) IBOutlet UILabel *phoneLable;

@end

@implementation AppointmentDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"预约挂号";
    _patient = [Patient sharedPatient];
    _schedule = [ScheduleDoctor sharedInstance];
    if (self.phone.length != 0) {
        self.phoneLable.text = @"预约手机:";
    }else{
        self.phoneLable.text = @"预约卡号:";
    }
    [self setLabelTextData];
    self.mainView.layer.cornerRadius = 5.0f;
    self.mainView.layer.masksToBounds = YES;
    self.mainView.layer.borderWidth = 0.8f;
    self.mainView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.appointmentButton.layer.cornerRadius = 5.0f;
    self.appointmentButton.layer.masksToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 预约
- (IBAction)appointmentEvent:(UIButton *)sender {
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    if (self.phone.length != 0) {
        NSDictionary *noCardParams = @{@"name" : self.name,
                                       @"sfz" : self.num,
                                       @"dhhm" : self.phone,
                                       @"pblsh" : _schedule.pblsh,
                                       @"hyh" : _schedule.hyh,
                                       };
        __weak typeof(self) weakSelf = self;
//        [HTTPSTool POSTRequestWithPath:XHNocardorder cookie:cookie params: noCardParams success:^(NSDictionary * _Nonnull JSON) {
//            //        XHLog(@"预约成功 -> JSON: %@", JSON)
//            [ZYToast showBottomText:@"预约成功!"];
//            NoCardAppointmentRecordController *noCardVc = [[NoCardAppointmentRecordController alloc] init];
//            noCardVc.isAlert =  NO;
//            noCardVc.phone = weakSelf.phone;
//            [weakSelf.navigationController pushViewController: noCardVc animated: YES];
//        }];
    }else{
        NSDictionary *param = @{@"id" : @(_patient.id),//病人编号
                                @"ygdm" : _schedule.ygdm,//医生编号
                                @"ksdm" : _schedule.ghksdm,//科室代码
                                @"jzrq" : [_schedule.jzrq substringToIndex:_schedule.jzrq.length-1],//日期
                                @"sdbz" : [_schedule.jzrq substringFromIndex:_schedule.jzrq.length-1],//上午：1，下午：2
                                @"orderID":_schedule.orderID
                           };
        __weak typeof(self) weakSelf = self;
        [HTTPSTool POSTRequestWithPath:XHOrder cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
            //        XHLog(@"预约成功 -> JSON: %@", JSON)
            [ZYToast showBottomText:@"预约成功!"];
            AppointmentRecordController *record = [[AppointmentRecordController alloc] init];
            [weakSelf.navigationController pushViewController:record animated:YES];
        }];
    }

}

- (void)setLabelTextData {
//    XHLog(@"patient: %@, \ndata: %@", _patientDataDict, _selectDataDict)
    self.depLabel.text = _schedule.depName;
    self.doctorLabel.text = _schedule.ygxm;
    NSString*subStr = [_schedule.jzrq substringToIndex:_schedule.jzrq.length-1];
    self.dateLabel.text = subStr;
    self.timeLabel.text = _schedule.jzsj;
    if (self.name.length != 0) {
        self.userLabel.text = self.name;
    }else{
        self.userLabel.text = _patient.name;
    }
    if (self.phone.length != 0) {
        self.numberLabel.text = self.phone;
    }else{
        self.numberLabel.text = _patient.bindNum;
    }
    
}

- (NSString *)dateString { // 日期  加 星期
    NSDate *date = [_schedule.jzrq dateStringWithFormat:@"yyyyMMdd"];
    NSString *weekday = [NSString stringWeekdayWithFromDate:date weekType:WeekdayDateStringTypeXingQi];
    NSString *string = [NSString stringAnalysisWithDate:date format:@"yyyy-MM-dd"];
    return [NSString stringWithFormat:@"%@ %@", string, weekday];
}

@end
